#include <iostream>
#include <algorithm>

//双向深搜
using namespace std;
typedef long long LL;

const int N = 46;

int n, m;
int W[N];
int weights[1 << 25], cnt = 1;
int ans = 0;

void dfs1(int u, int w, int k)
{
    if (u == k)
    {
        weights[cnt++] = w;
        return;
    }

    dfs1(u + 1, w, k);
    if ((LL)W[u] + w <= n)
    {
        dfs1(u + 1, W[u] + w, k);
    }
}

void dfs(int u, int w)
{
    if (u == m)
    {

        int l = 0, r = cnt - 1;
        while (l < r)
        {
            int mid = (l + r + 1) >> 1;
            if ((LL)weights[mid] + w <= n)
            {
                l = mid;
            }
            else
            {
                r = mid - 1;
            }
        }

        ans = max(ans, weights[l] + w);
        return;
    }

    dfs(u + 1, w);
    if ((LL)w + W[u] <= n)
    {
        dfs(u + 1, w + W[u]);
    }
}

int main()
{
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
    cin >> n >> m;
    for (int i = 0; i < m; i++)
    {
        cin >> W[i];
    }

    sort(W, W + m);
    reverse(W, W + m);

    int k = m / 2;
    dfs1(0, 0, k);

    sort(weights, weights + cnt);
    int cnt1 = 0;
    for (int i = 0; i < cnt;)
    {
        int j = i + 1;
        while (weights[i] == weights[j] && j < cnt)
        {
            j++;
        }
        weights[cnt1++] = weights[i];
        i = j;
    }

    cnt = cnt1;

    dfs(k, 0);

    cout << ans;
    return 0;
}
